package java07;

import java.util.*;
class Repo{
	public ArrayList<String> list = new ArrayList<String>();
	public Repo(String items) {
		String[] s = items.split(" ");
		for (int i = 0; i < s.length; i++) {
			list.add(s[i]);
		}
	}
	public int getSize(){
		return list.size();
	}
}
class Worker1 implements Runnable{
	private Repo repo1;
	public Worker1(Repo repo) {
		this.repo1 = repo;
	}
	public void run() {
		while (true) {
			synchronized (Worker1.class) {

				Worker1.class.notify();
				if(repo1.list.size()==0) break;
				System.out.println(Thread.currentThread().getName()+" finish "+repo1.list.remove(0));
				try {
					Worker1.class.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

class Worker2 implements Runnable{
	private Repo repo2;
	public Worker2(Repo repo) {
		this.repo2 = repo;
	}
	public void run() {
		while (true) {
			synchronized (Worker1.class) {
				Worker1.class.notify();
				if(repo2.list.size()==0) break;
				System.out.println(Thread.currentThread().getName()+" finish "+repo2.list.remove(0));
				try {
						Worker1.class.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			
		}
		
	}
}
public class Java76 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        Repo repo = new Repo(sc.nextLine());
        Thread t1 = new Thread(new Worker1(repo));
        Thread t2 = new Thread(new Worker2(repo));
        t1.start();
        Thread.yield();
        t2.start();
        sc.close();
	}
}
